//
//  ProductViewCell.m
//  AffectionBee
//
//  Created by 王炯 on 16/8/21.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "ProductViewCell.h"
#import "UIImageView+WebCache.h"



@interface ProductViewCell()

//(1)商品图片
@property (nonatomic,weak)UIImageView *productImg;
//(2)精选图片
@property (nonatomic,weak)UIImageView *specialImg;
//(3)商品名label
@property (nonatomic,weak)UILabel *nameLabel;
//(4)买一送一的image
@property (nonatomic,weak)UIImageView *buyOneImg;
//(5)规格label
@property (nonatomic,weak)UILabel *specLabel;
//(6)价格label
@property (nonatomic,weak)UILabel *priceLabel;
//(7)原价label
@property (nonatomic,weak)UILabel *marketLabel;

//7.1 原价上的划线
@property (nonatomic,weak)UIView *lineView;


//(8)添加购买按钮
@property (nonatomic,strong)UIButton *buyButton;

//(9)添加购买按钮或者是补货中label
@property (nonatomic,strong)UILabel *noGoodsLabel;

//商品的总存货数量
@property (nonatomic,assign)NSInteger storeNum;

//当前商品的购买数量
@property (nonatomic,assign)NSInteger hasBuyNum;



//(10)显示购买数量的label
@property (nonatomic,strong)UILabel *buyNumLabel;

//(11)添加减少物品的按钮
@property (nonatomic,strong)UIButton *deleteBuyButton;



@end

@implementation ProductViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
        //重新搭建界面
        [self setUpUI];
        
    }
    
    return self;
}

//重新搭建界面
-(void)setUpUI
{
    
    //(1)商品图片
    UIImageView *productImg = [[UIImageView alloc] init];
    
    self.productImg = productImg;
    
    [self.contentView addSubview:productImg];
    
    //添加商品图片的约束
    [productImg mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.leading.bottom.equalTo(self.contentView);
        
        make.width.equalTo(@80);
        
    }];
    
    
    //(2)精选图片
    UIImageView *specialImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jingxuan.png"]];
    
    
    self.specialImg = specialImg;
    
    [self.contentView addSubview:specialImg];
    
    //添加约束
    [specialImg mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.equalTo(self.contentView).offset(5);
        
        make.leading.equalTo(productImg.mas_trailing).offset(5);
        
        make.width.equalTo(@30);
        make.height.equalTo(@15);
        
        
    }];
    
    //(3)商品名label
    UILabel *nameLabel = [[UILabel alloc] init];
    self.nameLabel = nameLabel;
    
    //设置大小
    [self.nameLabel setFont:[UIFont systemFontOfSize:14]];
    
    [self.contentView addSubview:nameLabel];
    
    //添加约束
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).offset(5);
        
        make.leading.equalTo(specialImg.mas_trailing).offset(2);
        
        make.trailing.equalTo(self.contentView);
        
    }];
    
    
    //(4)买一送一label(可以为无)
    UIImageView *buyOneImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buyOne.png"]];
    self.buyOneImg = buyOneImg;
    [self.contentView addSubview:buyOneImg];
    
    //设置约束和大小
    [buyOneImg mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(specialImg.mas_bottom).offset(2);
        make.leading.equalTo(productImg.mas_trailing).offset(5);
        
        make.width.equalTo(@35);
        make.height.equalTo(@15);
        
    }];
    
    
    //(5)价格label
    UILabel *priceLabel = [[UILabel alloc] init];
    
    self.priceLabel = priceLabel;
    
    //设置大小
    [priceLabel setFont:[UIFont systemFontOfSize:14]];
    
    //设置颜色
    [priceLabel setTextColor:[UIColor redColor]];
    
    
    [self.contentView addSubview:priceLabel];
    
    //设置约束
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(productImg.mas_trailing).offset(5);
        make.bottom.equalTo(self.contentView);
        
    }];
    
    //(6)原价label
    UILabel *marketLabel = [[UILabel alloc] init];
    
    self.marketLabel = marketLabel;
    
    //设置大小
    [marketLabel setFont:[UIFont systemFontOfSize:14]];
    
    //设置颜色
    [marketLabel setTextColor:[UIColor grayColor]];
    
    [self.contentView addSubview:marketLabel];
    
    //设置约束
    [marketLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(priceLabel.mas_trailing).offset(5);
        make.bottom.equalTo(self.contentView);
    }];

    
    //(7)规格label
    UILabel *specLabel = [[UILabel alloc] init];
    
    self.specLabel = specLabel;
    
    //设置大小
    [specLabel setFont:[UIFont systemFontOfSize:12]];
    
    //设置颜色
    [specLabel setTextColor:[UIColor grayColor]];
    
    [self.contentView addSubview:specLabel];
    
    //设置约束
    [specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(productImg.mas_trailing).offset(5);
        make.bottom.equalTo(priceLabel.mas_top).offset(-5);
    }];
    
    
}

//懒加载购买按钮
-(UIButton *)buyButton
{
    if (_buyButton == nil)
    {
        
        _buyButton = [[UIButton alloc] init];
        
        //设置背景图片
        [_buyButton setBackgroundImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
        
        //设置大小自适应
        [_buyButton sizeToFit];
        
        //添加购买button的触发事件
        [_buyButton addTarget:self action:@selector(BuyProduct) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _buyButton;
}


//购买商品的触发事件
-(void)BuyProduct
{
   
    //判断有没有达到上限
    if (self.hasBuyNum >= self.storeNum)
    {
#warning 超过了总数量了
        NSLog(@"不能再买了,超过总数了");
        
        return;
    }
    
    //购买商品数加一
    self.hasBuyNum ++;
    
    
    //购买数量label解禁隐藏
    self.buyNumLabel.hidden = NO;
    
    //减少按钮解禁隐藏
    self.deleteBuyButton.hidden = NO;
    
    
    //设置文本
    self.buyNumLabel.text = [NSString stringWithFormat:@"%ld",(long)self.hasBuyNum];
    
    
    //发通知给底部的购物车(加货物)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SuperMarketAddOne" object:nil];
    
    
    //发通知给主VC，把当前cell的imageView传递过去
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CellImageAnimate" object:self.productImg];
    
    
    //更新数据库中的商品库存数量和购买量
    //(1)取出库存商品数和购买量
    self.proModel.hasBuyNum = [NSString stringWithFormat:@"%ld",(long)self.hasBuyNum];
    
    
    [DBManager UpdateDataWithModel:self.proModel];
    
}

//懒加载购买数量的label
-(UILabel *)buyNumLabel
{
    if (_buyNumLabel == nil)
    {
        
        _buyNumLabel = [[UILabel alloc] init];
        
        //设置文本的大小
        [_buyNumLabel setFont:[UIFont systemFontOfSize:14]];
        
        //默认隐藏
        _buyNumLabel.hidden = YES;
        
    }
    
    return _buyNumLabel;
}

//懒加载减少物品按钮
-(UIButton *)deleteBuyButton
{
    if (_deleteBuyButton == nil)
    {
        _deleteBuyButton = [[UIButton alloc] init];
        
        //设置背景图片
        [_deleteBuyButton setBackgroundImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
        
        //设置自适应
        [_deleteBuyButton sizeToFit];
        
        //添加button的触发事件
        [_deleteBuyButton addTarget:self action:@selector(reduceProduct) forControlEvents:UIControlEventTouchUpInside];
        
        //默认隐藏
        _deleteBuyButton.hidden = YES;
        
    }
    
    return _deleteBuyButton;
}


//减少物品按钮
-(void)reduceProduct
{
    //购买商品数量减1
    self.hasBuyNum -- ;
    
    //重新设置数据
    //设置文本
    self.buyNumLabel.text = [NSString stringWithFormat:@"%ld",(long)self.hasBuyNum];
    
    //发通知给底部的购物车(减货物)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SuperMarketReduceOne" object:nil];
    
    
    //判断有没有为0
    if (self.hasBuyNum == 0)
    {
        //将减少按钮和显示数量label隐藏
        self.buyNumLabel.hidden = YES;
        
        self.deleteBuyButton.hidden = YES;
    }
    
    //更新数据库中的商品库存数量和购买量
    //(1)取出库存商品数和购买量
    self.proModel.hasBuyNum = [NSString stringWithFormat:@"%ld",(long)self.hasBuyNum];
    
    
    [DBManager UpdateDataWithModel:self.proModel];
    
}


//懒加载补货中label
-(UILabel *)noGoodsLabel
{
    if (_noGoodsLabel == nil)
    {
     
        _noGoodsLabel = [[UILabel alloc] init];
        
        //设置文本
        [_noGoodsLabel setText:@"补货中"];
        
        //设置文本大小
        [_noGoodsLabel setFont:[UIFont systemFontOfSize:14]];
        
        //设置文本颜色
        [_noGoodsLabel setTextColor:[UIColor redColor]];
        
        
    }
    return _noGoodsLabel;
}


//重写model，设置数据
-(void)setProModel:(ProductModel *)proModel
{
    _proModel = proModel;
    
    
    
    //(1)设置商品图片
    [self.productImg sd_setImageWithURL:[NSURL URLWithString:proModel.img] placeholderImage:[UIImage imageNamed:@"v2_placeholder_full_size"]];
    
    //(2)设置商品名
    self.nameLabel.text = proModel.name;
    
    
    //(3)设置买一赠送的隐藏或显现
    if ([proModel.pm_desc isEqualToString:@"买一赠一"])
    {
        self.buyOneImg.hidden = NO;
    }
    else
    {
        self.buyOneImg.hidden = YES;
    }
    
    //(4)设置价格数据
    self.priceLabel.text = [NSString stringWithFormat:@"$%@",proModel.partner_price];
    
    //(5)设置原价数据(可以为无)
    //判断原价与现价是否相等
    if (!([proModel.market_price isEqualToString:proModel.partner_price]))
    {
        self.marketLabel.hidden = NO;
        self.marketLabel.text = [NSString stringWithFormat:@"$%@",proModel.market_price];
        
    }
    else
    {
        self.marketLabel.hidden = YES;
    }
    
    //(6)设置规格数据
    self.specLabel.text = proModel.specifics;
    
    //(7)根据商品总库存数量来设置button或label
    self.storeNum = [proModel.number integerValue];

    //根据本地数据更新已经购买数
    
    self.hasBuyNum = [proModel.hasBuyNum integerValue];
    
    
    if (self.storeNum <= 0)
    {
        
        //将添加按钮从cell上移除
        [self.buyButton removeFromSuperview];
        
        //将购买商品数量的label从cell上移除
        [self.buyNumLabel removeFromSuperview];
        
        //将减少按钮从cell上移除
        [self.deleteBuyButton removeFromSuperview];
        
        
        
        //显示补货中
        //添加到view上
        [self.contentView addSubview:self.noGoodsLabel];
        
        //设置约束
        [self.noGoodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.trailing.equalTo(self.contentView).offset(-5);
            make.bottom.equalTo(self.contentView);
        }];
        
    }
    else
    {
        
        //将补货中label从cell上移除
        [self.noGoodsLabel removeFromSuperview];
        
        
        //显示添加按钮
        //添加到view上
        [self.contentView addSubview:self.buyButton];
        
        
        
        
        //设置约束
        [self.buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.trailing.equalTo(self.contentView).offset(-5);
            make.bottom.equalTo(self.contentView);
        }];
        
        
        
        //将购买数量label添加到界面上
        [self.contentView addSubview:self.buyNumLabel];
        
        
        //设置约束
        [self.buyNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.trailing.equalTo(self.buyButton.mas_leading).offset(-8);
            make.centerY.equalTo(self.buyButton);
            
        }];
        
        
        //将减少物品数量按钮添加到界面上
        [self.contentView addSubview:self.deleteBuyButton];
        
        //设置约束
        [self.deleteBuyButton mas_makeConstraints:^(MASConstraintMaker *make){
            
            make.centerY.equalTo(self.buyButton);
            make.trailing.equalTo(self.buyNumLabel.mas_leading).offset(-8);
            
        }];
        
        
        //判断已经购买数时候大于0来判断显示或者隐藏购买数量label和减少物品button
        if (self.hasBuyNum > 0)
        {
            //显示
            //购买数量label解禁隐藏
            self.buyNumLabel.hidden = NO;
            
            //减少按钮解禁隐藏
            self.deleteBuyButton.hidden = NO;
            
            
            //设置文本
            self.buyNumLabel.text = [NSString stringWithFormat:@"%ld",(long)self.hasBuyNum];
        }
        else
        {
            //隐藏
            //购买数量label隐藏
            self.buyNumLabel.hidden = YES;
            
            //减少按钮隐藏
            self.deleteBuyButton.hidden = YES;
        }
        
        
    }
    
}

//布局子界面
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //添加划线到原价上
    //判断原价label有没有隐藏
    //首先将cell上的旧划线去掉
    [self.lineView removeFromSuperview];
    
    if (!(self.marketLabel.isHidden))
    {
        //添加一个划线到原价label上
        UIView *lineView = [[UIView alloc] init];
        
        self.lineView = lineView;
        
        lineView.backgroundColor = [UIColor grayColor];
        
        //将lineView添加到label上
        [self.marketLabel addSubview:lineView];
        
        
#warning font.xHeight:marketLabel字体的一半高度
        CGFloat marketLabelHeight = self.marketLabel.font.xHeight;
        
        
        //设置LineView的大小和约束
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.height.equalTo(@2);
            make.width.equalTo(self.marketLabel.mas_width);
            
            
            make.bottom.equalTo(self.marketLabel.mas_bottom).equalTo(@(-marketLabelHeight));
            
            make.leading.equalTo(self.marketLabel);
        }];
    }
    
}



@end
